require.config({
    paths: {
        'jquery': 'http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js',
        'vue': '../encaptool/encvue',
        'iview': '../utiltool/iview/iview',
        'bmap': 'http://api.map.baidu.com/api?v=2.0&ak=9whTPxXrl8zvW3gjiAaoPa59gFekKK9I',
        'async': '../utiltool/requirejsplugins/src/async',
        'axios': '../encaptool/encaxios'
    },
    shim: {
        'iview': {
            deps: ['vue']
        },
        'vueEventCalendar': {
            deps: ['vue']
        },
        'bmap': {
            deps: ['jquery'],
            exports: 'BMap'
        }
    }
})

require(['vue', 'iview', 'axios', 'jquery', 'async!bmap'], function (Vue, iview, axios)  {
    Vue.use(iview);
   
    new Vue({
        el: "#factory_management",
        data() {
            return {
                map: {},
                search: "",
                listData:[],
                points:[],
                showMoal: false,
                reaschplace: "",
                local: null,
                addfactorycity: null,
                placelist: {
                    points: [],
                    address: [] 
                },
                newInfo:{
                    factoryname:"",
                    longitude: 116.828624,
                    latitude: 40.011752
                }
            }
        },
        mounted() {
            this.initMap();
        },
        watch: {
         //    reaschplace: 'fuzzyQueryLocal'
        },
        methods: {
            initMap: function () {
                axios.call(this,function (res) {
                    if(res.code === 0){
                        this.listData = res.data;
                        res.data.forEach(function (r) {
                            this.points.push(new BMap.Point(r.longitude, r.latitude));
                        }.bind(this));
                        this.setMap();
                    }else{
                        this.setMap();
                    }
                }.bind(this),{
                    setting:{
                        method: "GET",
                        url: "/YF/public/api/getFactory"
                    }
                })
            },
            setMap(){
                var map = new BMap.Map("factorymap");
                var centerAndZomm = map.getViewport(this.points);
                map.centerAndZoom(centerAndZomm.center, centerAndZomm.zoom);
                var icon = new BMap.Icon("/YF/public/static/img/marker1.png", new BMap.Size(30, 30));
                this.points.forEach(function (r) {
                    var marker = new BMap.Marker(r, {
                        icon: icon
                    });
                    map.addOverlay(marker);
                }.bind(this));
                this.map = map;
            },
            addFactory(){
                this.showMoal = true;
            },
            emitInfo(){
                if (!this.reaschplace){
                    return;
                } 
                if (!this.newInfo.longitude) {
                    return;
                } 
                if (!this.newInfo.latitude) {
                    return;
                }
                axios.call(this, function (res) {
                    this.points = [];
                    this.initMap();
                }.bind(this)    ,{
                    setting: {
                        method: "POST",
                        url: "/YF/public/api/addFactory"
                    },
                    data: {
                        factoryname: this.reaschplace,
                        longitude: this.newInfo.longitude,
                        latitude: this.newInfo.latitude
                    }
                })
            },
            handleClose: function (ld) {
                axios.call(this, function (res) {
                    this.listData = [];
                    this.points = [];
                    this.initMap();
                }.bind(this), {
                        setting: {
                            method: "POST",
                            url: "/YF/public/api/deleteFactory"
                        },
                        data: ld
                    })
            },
            fuzzyQueryLocal: function () {
                this.local = new BMap.LocalSearch(this.map, { //智能搜索
                    onSearchComplete: this.queryCallback
                });
                  this.local.search(this.reaschplace);
            },
            queryCallback: function (res) {
                if(this.local.getStatus() == BMAP_STATUS_SUCCESS){
                    this.placelist.address = [];
                    this.placelist.points = [];
                    res.zr.forEach(function (r) {
                        var str = r.city + ' - ' + r.address + ' - ' + r.title
                        this.placelist.address.push(str);
                        this.placelist.points.push(r.point);
                    }.bind(this));
                }else{
                    this.placelist.address = [];
                    this.placelist.points = [];
                }
            },
            setPoint: function (value) {
                for(var i = 0; i < this.placelist.address.length; i ++){
                    if (this.placelist.address[i] === value){
                        this.newInfo.factoryname = value;
                        this.newInfo.latitude = this.placelist.points[i].lat;
                        this.newInfo.longitude = this.placelist.points[i].lng;
                    }
                }
            }
        },
        computed: {
        }
    });
});